Skip to content

Decide pass or fail from the predictions ​

A model says what it sees; the rules of the inspection say whether the product is good. In this guide you write that rule for packs of biscuits: a pack passes when the model finds its four biscuits, and fails otherwise. The same few lines decide on any model: count objects, check a tag, require a confidence.

What you will learn

  • Where the decision goes in an inspection flow
  • How to write a rule on the predictions of a model with a function node
  • How to try the rule on a whole dataset

Before you start

You need a flow that runs a model, as in Run a trained model on an image. Here it streams the «DemoCookies» dataset through a segmentation model that finds each biscuit, with a «start» and a «stop» inject node; see Send dataset images through a flow. The predictions are in msg.inference.

1. Add a function node after the model ​

Type function in «filter nodes», drag a function node after the inferencer and wire them together. The rule goes inside it.

Add a function node after the model

2. Write the rule ​

Double-click the node, name it and write the rule above return msg;:

js
const biscuits = msg.inference.filter((b) => b.confidence >= 0.5).length;
msg.payload = biscuits === 4 ? "OK" : "NOK";
msg.topic = biscuits + " biscuits";
return msg;

The first line counts the predictions the model is at least 50 % sure of. The second turns the count into the verdict, in msg.payload. The third keeps the count in msg.topic, which the debug panel shows next to each message.

Write the rule

3. Show the verdicts ​

Click «Done» and add a debug node after the function node. It shows msg.payload, the verdict.

4. Try it on the whole dataset ​

Click «Deploy» and press the «start» inject node. The packs of the dataset go through the model one after another, and the debug panel shows the verdict of each one with its count of biscuits. Trying a rule on a whole dataset this way shows how it would decide on the line before it decides anything.

Try it on the whole dataset

5. Stop the stream ​

Press «stop» when you have seen enough.

Rules for other models ​

The predictions of every model arrive in the same shape, so rules for other models are as short:

The product passes when…Rule
A classification model says it is goodmsg.payload = msg.inference.tag === "ok" && msg.inference.confidence >= 0.8 ? "OK" : "NOK";
A detection model finds no defectmsg.payload = msg.inference.some((d) => d.tag === "defect") ? "NOK" : "OK";
No defect covers more than 1 % of the imagemsg.payload = msg.inference.every((d) => d.area < 0.01) ? "OK" : "NOK";

Acting on the verdict ​

A switch node sends each message out of a different output depending on msg.payload, so that the OK and NOK packs take different paths: one to a counter, the other to the signal that rejects the product, or both to a panel for the operators.

Next steps ​

Recorded on Rosepetal release 1.8.15, Node-RED 5.0.10.